<?php
include '../config/config.php';
include '../lib/promo.php';
include '../lib/produk.php';

$config=new config();
$config->connect();

session_start();
if (!isset($_SESSION['MM_Username'])){
	$togo="../login.php";
	header ("Location:$togo");
}

if (ISSET($_GET["id"])){
$id=$_GET["id"];
//echo $id;
$Promo_Dao=new Promo_Dao();
$promo=$Promo_Dao->get_id($id);
//echo $promo->id_produk;

}

$Produk_Dao=new Produk_Dao();
$produk=$Produk_Dao->get_idmember($_SESSION['MM_Id']);

if (ISSET($_POST["ubah_promo"])){

	$Promo_Dao=new Promo_Dao();
	$promo = new Promo();
	$promo->id_promo = $_POST['id_promo'];
	$promo->id_produk = $_POST['id_produk'];
	$promo->tgl_mulai = $_POST['tgl_mulai'];
	$promo->tgl_selesai = $_POST['tgl_selesai'];
	$promo->harga_promo = $_POST['harga_promo'];
	$promo->keterangan_promo = $_POST['keterangan_promo'];

  if ($_POST['tgl_mulai']!="" && $_POST['tgl_selesai']!="" && $_POST['harga_promo']!=""){

	$Promo_Dao->ubah_promo($promo);
	header("Location:daftar_promo.php");
	
	} else {
		echo "Isikan data dengan lengkap terlebih dahulu!";
	}
	
}

?>
<?php include("header_member.php") ?>
<?php include("menu_kiri_member.php") ?>

<div id="body">
<div class="topbox">
                  Ubah Promo:
                  <br>
				  <br>
				  <form method="post" name="form1" action="ubah_promo.php">
				  <input type="hidden" name="ubah_promo">
				  <input type="hidden" name="id_promo" value = "<?php echo $promo->id_promo;?>">
                  <table class="table" cellspacing=0 cellpadding=3>
				   <tr>
					<td>Produk</td>
					<td>:</td>
					<td><select name="id_produk">
					<?php
						foreach($produk as $Produk){
						
							$selected="";
							if($promo->id_produk == $Produk->id_produk){
							$selected = "selected";
							}
					?>
							<option value="<?php echo $Produk->id_produk;?>" <?php echo $selected;?>><?php echo $Produk->nama_produk." - Rp".number_format($Produk->harga, 2, '.', ',');?></option>
					
						<?php }?>
					
					</select></td>
				  </tr>
				  <tr>
					<td>Tgl Mulai*</td>
					<td>:</td>
					<td><input type="text" name="tgl_mulai" id="tgl_mulai" value = "<?php echo $promo->tgl_mulai;?>"></td>
				  </tr>
				  <tr>
					<td>Tgl Selesai*</td>
					<td>:</td>
					<td><input type="text" name="tgl_selesai" id="tgl_selesai" value = "<?php echo $promo->tgl_selesai; ?>"></td>
				  </tr>
				  <tr>
					<td>Harga Promo*</td>
					<td>:</td>
					<td><input type="text" name="harga_promo" value = "<?php echo $promo->harga_promo; ?>"></td>
				  </tr>
				  <tr>
					<td>Keterangan Promo</td>
					<td>:</td>
					<td><input type="text" name="keterangan_promo" value = "<?php echo $promo->keterangan_promo; ?>" size=64></td>
				  </tr>
				  </table>
				  <br>
				  <input type="submit" value="Simpan" onclick="return validasi();"><input type="button" value=" Batal " onClick="window.location.href='daftar_promo.php'">
				  </form>
                  <br>                
</div> 
<div class="topbox">
* Data harus diisi.
</div>            
</div>
          
         <div class="clear"></div>
         <?php include("../footer.php") ?>
		 
<script>
function validasi(){
	var a = document.form1.id_produk.value;
	var b = document.form1.tgl_mulai.value;
	var c = document.form1.tgl_selesai.value;
	var d = document.form1.harga_promo.value;
	if(a=="" || b=="" || c=="" || d==""){
		alert('Isikan data terlebih dahulu');
		return false;
	}
}
</script>
		